import { createRouter, createWebHistory } from 'vue-router';
import { useUserStore } from '@/stores/user';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

// NProgress 配置
NProgress.configure({
  showSpinner: false,
});

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    // { path: '/', redirect: '/home' },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/Layout/Home/index.vue'),
          meta: { title: '首页' },
        },
        {
          path: '/article',
          component: () => import('@/views/Layout/Article/index.vue'),
          meta: { title: '健康百科' },
        },
        {
          path: '/notify',
          component: () => import('@/views/Layout/Notify/index.vue'),
          meta: { title: '消息通知' },
        },
        {
          path: '/user',
          component: () => import('@/views/Layout/User/index.vue'),
          meta: { title: '个人中心' },
        },
      ],
    },
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue'),
      meta: { title: '登录' },
    },
    {
      path: '/user/patient',
      component: () => import('@/views/Layout/User/PatientPage.vue'),
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '极速问诊' },
    },

    {
      path: '/consult/dept',
      component: () => import('@/views/Consult/ConsultDept.vue'),
      meta: { title: '科室选择' },
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' },
    },
    {
      path: '/consult/patient',
      component: () => import('@/views/Consult/ConsultPatient.vue'),
      meta: { title: '选择患者' },
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' },
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      beforeEnter(to) {
        console.log('=====>', to.query);
        if (to.query.payResult === 'false') return '/user/consult';
      },
    },
    {
      path: '/user/consult',
      component: () => import('@/views/Layout/User/ConsultPage.vue'),
      meta: { title: '问诊记录' },
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/Layout/User/ConsultDetail.vue'),
      meta: { title: '问诊详情' },
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/LoginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' },
    },

    { path: '/test', component: () => import('@/views/Test/TestPage.vue') },
  ],
});

// 全局路由前置守卫
router.beforeEach((to) => {
  // 开启加载提示
  NProgress.start();

  // 条件：用户没有登录，并且访问的不是 /login 页面
  const userStore = useUserStore();
  // 不需要登录的页面白名单
  const wihteList = ['/login', '/login/callback'];
  // 如果没有登录且访问的不是 /login 页面，去登录
  if (!userStore.user?.token && !wihteList.includes(to.path)) return '/login';
  // 否则放行。放行可以返回 true 也可以不处理
  return true;
  // if (!userStore.user?.token && to.path !== '/login') return '/login';
  // // 放行
  // return true;
  // if (!userStore.user?.token && to.path !== '/login') {
  //   return '/login';
  // } else {
  //   // 放行
  //   return true;
  // }
});

// 全局路由后置守卫
router.afterEach((to) => {
  // 关闭加载提示
  NProgress.done();

  // 设置页面标题
  if (to.meta.title) {
    document.title = '优医问诊-' + to.meta.title;
  } else {
    document.title = '优医问诊';
  }
});

export default router;
